<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>新增字典项</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css" />
    <script src="../../../component/layui/layui.js"></script>
    <script src="../../../component/pear/pear.js"></script>
</head>

<body>
    <form class="layui-form" action="" lay-filter="form" autocomplete="off">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">父级项</label>
                    <div class="layui-input-block">
                        <div id="pId"></div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">字典值<span class="layui-font-red">*</span></label>
                    <div class="layui-input-block">
                        <input type="text" name="value" lay-verify="required" placeholder="请输入字典值" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序<span class="layui-font-red">*</span></label>
                    <div class="layui-input-block">
                        <input type="number" name="sort" lay-verify="required" placeholder="请输入排序" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态<span class="layui-font-red">*</span></label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="isActive" lay-skin="switch" lay-text="启用|禁用" lay-filter="isActive"
                            checked="checked">
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-sm pear-btn-primary" lay-submit="" lay-filter="save">
                    <i class="icon pear-icon pear-icon-select-bold"></i>
                    提交
                </button>
                <button type="button" lay-click="layerclose" class="pear-btn pear-btn-sm">
                    <i class="icon pear-icon pear-icon-close-bold"></i>
                    关闭
                </button>
            </div>
        </div>
    </form>
</body>

</html>

<script>
    layui.use(['form', 'jquery', 'context', 'permission', 'popup', 'common', 'button', 'util', 'xmSelect'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var context = layui.context;
        var popup = layui.popup;
        var common = layui.common;
        var button = layui.button;
        var util = layui.util;
        var xmSelect = layui.xmSelect;

        var basePath = context.get("base-path");

        var type = common.getQueryString("type");

        //加载父级项下拉树
        common.loadXmSelectTree({
            divId: "#pId",
            name: "pId",
            url: basePath + '/api/system/dict/gettreelist',
            data: JSON.stringify({ "content": type }),
        })

        form.on('submit(save)', function (data) {
            var btn = button.load({ elem: 'button[lay-filter=save]' });
            data.field.type = type;
            data.field.isActive = data.field.isActive == "on" ? true : false;
            $.ajax({
                url: basePath + '/api/system/dict/add',
                data: JSON.stringify(data.field),
                success: function (result) {
                    btn.stop(function () {
                        if (result.succeeded) {
                            popup.success("添加成功", function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.window.loadTreeTable();
                            });
                        } else {
                            popup.failure(JSON.stringify(result.errors));
                        }
                    })
                }
            })
            return false;
        });


        util.event('lay-click', {
            layerclose: function (othis) {//关闭当前弹窗
                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
            },
        });
    })
</script>